'''
Description: 
Author: notplus
Date: 2021-12-13 23:13:49
LastEditors: notplus
LastEditTime: 2021-12-14 18:59:17
FilePath: /markov.py

Copyright (c) 2021 notplus
'''

import pickle
import numpy as np
from numpy.core.fromnumeric import sort
from pyproj import Transformer
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from datetime import datetime, timedelta

import utils
from extra_stay_points import ActivityCluster, User, Trajectory, StayPoint
from dbscan import DBSCAN

class Trans(object):
    def __init__(self, start_point, end_point):
        self.start_point = start_point
        self.end_point = end_point
        

with open('all_activity_cluster_128.pickle', 'rb') as f:
    all_activity_cluster = pickle.load(f)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for j in range(30):
    x = []
    y = []
    z = []
    
    for act in all_activity_cluster[j].activities:
        # if (act.arv_t < datetime(2011,9,1) and act.arv_t > datetime(2010,1,1)):
        if True:
            transformer = Transformer.from_crs("epsg:4326", "epsg:32650") 
            xt, yt = transformer.transform(act.lat, act.lng)
            x.append(xt)
            y.append(yt)
            z.append((act.arv_t - act.arv_t.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 3600)

    ax.scatter(x, y, z, marker='^')

for j in range(0):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    x = []
    y = []
    z = []
    
    for act in all_activity_cluster[j].activities:
        transformer = Transformer.from_crs("epsg:4326", "epsg:32650") 
        xt, yt = transformer.transform(act.lat, act.lng)
        x.append(xt)
        y.append(yt)
        z.append((act.arv_t - act.arv_t.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 3600)

    ax.scatter(x, y, z, marker='^')

next_activities = []
for i in range(5):
    for act in all_activity_cluster[i].next_activities:
        next_activities.append(act.id)

unique, counts = np.unique(next_activities, return_counts=True)

unique_count = {}

for i in range(len(unique)):
    unique_count[unique[i]] = counts[i]

unique_count = sorted(unique_count,key=unique_count.get,reverse=True)

trans = []
for i in range(len(all_activity_cluster)):
    for j in range(len(all_activity_cluster[i].next_activities)):
        if (all_activity_cluster[i].activities[j].arv_t > datetime(2011,7,1)):
            if all_activity_cluster[i].next_activities[j].id in unique_count[:]:
                trans.append(Trans(all_activity_cluster[i].activities[j], all_activity_cluster[i].next_activities[j]))

place = []

f = open('trans_128_model_true.csv', 'w')

for i in range(len(trans)):
    if trans[i].start_point.id not in place:
        place.append(trans[i].start_point.id)
    if trans[i].end_point.id not in place:
        place.append(trans[i].end_point.id)
    
    # print('%d: %d -> %d'%(i, trans[i].start_point.id, trans[i].end_point.id))
    print('%d,%d'%(trans[i].start_point.id, trans[i].end_point.id))

    f.write('%d,%d\n'%(trans[i].start_point.id, trans[i].end_point.id))
f.close()

for i in range(15):
    print('id: %d, lat: %f, lng: %f'%(all_activity_cluster[i].activities[0].id, all_activity_cluster[i].lat, all_activity_cluster[i].lng))

print('')